<?php

/**
 * This is the model class for table "izin".
 *
 * The followings are the available columns in table 'izin':
 * @property string $id
 * @property string $perusahaan_id
 * @property string $izin_kategori_id
 * @property integer $nomor_sk
 * @property string $tentang
 * @property string $tanggal_terbit
 * @property string $tanggal_berakhir
 * @property string $penanggung_jawab
 * @property string $kode_izin_usaha
 * @property string $kapasitas_pengolahan
 * @property string $lokasi
 * @property string $provinsi_id
 * @property string $status_izin
 * @property string $upload_dokumen
 * @property string $created
 * @property string $modified
 * @property integer $created_by
 * @property integer $modified_by
 *
 * The followings are the available model relations:
 * @property IzinKategori $izinKategori
 * @property Perusahaan $perusahaan
 * @property Provinsi $provinsi
 */
class Izin extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
        public $jenis_usaha;


        public function tableName()
	{
		return 'izin';
	}
        
        public function scopes() {
            return array(
                'homepage' => array(
                    'with' => array('provinsi'),
                    'condition' => "provinsi.koordinat!='' AND t.provinsi_id!=''",
                    'group' => 't.provinsi_id'
                ),
                'isMB' => array(
                    'with' => array(
                            'provinsi'=>array(
                                'select'=>'*',
                            ),
                            'perusahaan'=>array(
                                'select'=>'jenis_usaha',
                            ),
                        ),
                    'condition' => "provinsi.koordinat!='' AND t.provinsi_id!='' AND perusahaan.jenis_usaha='Pengolahan MB'",
                    'group' => 't.provinsi_id',
                ),
                'isGB' => array(
                    'with' => array(
                            'provinsi'=>array(
                                'select'=>'*',
                            ),
                            'perusahaan'=>array(
                                'select'=>'jenis_usaha',
                            ),
                        ),
                    'condition' => "provinsi.koordinat!='' AND t.provinsi_id!='' AND perusahaan.jenis_usaha='Pengolahan GB'",
                    'group' => 't.provinsi_id',
                ),
                'isHO' => array(
                    'with' => array(
                            'provinsi'=>array(
                                'select'=>'*',
                            ),
                            'perusahaan'=>array(
                                'select'=>'jenis_usaha',
                            ),
                        ),
                    'condition' => "provinsi.koordinat!='' AND t.provinsi_id!='' AND perusahaan.jenis_usaha='Pengolahan HO'",
                    'group' => 't.provinsi_id',
                ),
            );
        }

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('perusahaan_id,jenis_izin,kapasitas_pengolahan,provinsi_id,kabupaten_id,satuan_id', 'required'),
			array('perusahaan_id', 'length', 'max'=>11),
			array('alamat_kilang', 'length', 'max'=>200),
			array('tentang, nomor_sk,lokasi', 'length', 'max'=>100),
			array('penanggung_jawab, kode_izin_usaha', 'length', 'max'=>50),
			array('kapasitas_pengolahan', 'length', 'max'=>20),
			array('provinsi_id', 'length', 'max'=>2),
			array('status_izin', 'length', 'max'=>13),
			array('upload_dokumen', 'length', 'max'=>255),
			array('tanggal_terbit, tanggal_berakhir, created, modified', 'safe'),
//			array('upload_dokumen', 'file', 'allowEmpty'=>false, 'types'=>'pdf', 'message'=>'Upload dokumen terlebih dahulu dan pastikan file dalam bentuk PDF (.pdf).', 'on'=>'insert'),
//			array('upload_dokumen','safe','on'=>'insert'),
                        // The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, perusahaan_id, jenis_izin, nomor_sk, tentang, tanggal_terbit, tanggal_berakhir, penanggung_jawab, kode_izin_usaha, kapasitas_pengolahan, lokasi, provinsi_id, status_izin, upload_dokumen, created, modified, created_by, modified_by', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			//'izinKategori' => array(self::BELONGS_TO, 'IzinKategori', 'izin_kategori_id'),
			'perusahaan' => array(self::BELONGS_TO, 'Perusahaan', 'perusahaan_id'),
			'satuan' => array(self::BELONGS_TO, 'Satuan', 'satuan_id'),
			'kabupaten' => array(self::BELONGS_TO, 'Kabupaten', 'kabupaten_id'),
			'provinsi' => array(self::BELONGS_TO, 'Provinsi', 'provinsi_id'),
			'checklistAdmin' => array(self::HAS_MANY, 'ChecklistAdmin', 'izin_id'),
			'checklistEvaluasi' => array(self::HAS_MANY, 'ChecklistEvaluasi', 'izin_id'),
			'checklistPelaporan' => array(self::HAS_MANY, 'ChecklistPelaporan', 'izin_id'),
			'checklistPerizinan' => array(self::HAS_MANY, 'ChecklistPerizinan', 'izin_id'),
			'checklistSurat' => array(self::HAS_MANY, 'ChecklistSurat', 'izin_id'),
			'checklistTeknis' => array(self::HAS_MANY, 'ChecklistTeknis', 'izin_id'),
                        'checklistLain' => array(self::HAS_MANY, 'ChecklistTeknis', 'izin_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'perusahaan_id' => 'Perusahaan',
			'jenis_izin' => 'Jenis Izin',
			'nomor_sk' => 'Nomor SK',
			'tentang' => 'Tentang',
			'tanggal_terbit' => 'Tanggal Terbit',
			'tanggal_berakhir' => 'Tanggal Berakhir',
			'penanggung_jawab' => 'Penanggung Jawab',
			'kode_izin_usaha' => 'Kode Izin Usaha',
			'kapasitas_pengolahan' => 'Kapasitas Disain',
			'alamat_kilang' => 'Alamat Lengkap Kilang',
			'lokasi' => 'Nama Kilang',
			'kabupaten_id' => 'Kabupaten',
			'provinsi_id' => 'Provinsi',
			'status_izin' => 'Status Izin',
			'satuan_id' => 'Satuan',
			'upload_dokumen' => 'Upload Dokumen',
			'created' => 'Created',
			'modified' => 'Modified',
			'created_by' => 'Created By',
			'modified_by' => 'Modified By',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('perusahaan.nama_perusahaan',$this->perusahaan_id,true);
		$criteria->compare('perusahaan.jenis_usaha',$this->jenis_usaha,true);
		$criteria->compare('t.jenis_izin',$this->jenis_izin,true);
		$criteria->compare('t.lokasi',$this->lokasi,true);
		$criteria->compare('provinsi.nama',$this->provinsi_id,true);
		$criteria->compare('t.status_izin',$this->status_izin,true);
                $criteria->with = array(
                    'perusahaan'=>array(
                        'select'=>'nama_perusahaan,jenis_usaha'
                    ),
                    'provinsi'=>array(
                        'select'=>'nama'
                    ));
                $criteria->together = true;
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Izin the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
        
        public function beforeSave()
        {
            if($this->isNewRecord)
            {
                $this->created = new CDbExpression('NOW()');
                $this->modified = new CDbExpression('NOW()');
                $this->created_by = Yii::app()->user->id;
                $this->modified_by = Yii::app()->user->id;
            } else {
                $this->modified = new CDbExpression('NOW()');
                $this->modified_by = Yii::app()->user->id;
            }
            return parent::beforeSave();
        }
}
